import { createRouter, createWebHistory } from 'vue-router'
import PkIndexView from '../view/pk/PkIndexView.vue'
import RecordIndexView from '../view/record/RecordIndexView'
import RecordContentView from '../view/record/RecordContentView'
import RanklistIndexView from '../view/ranklist/RanklistIndexView'
import UserBotIndexView from '../view/users/bots/UserBotIndexView'
import NotFound from '../view/error/NotFound'
import UserAccountLoginView from '../view/users/account/UserAccountLoginView'
import UserAccountRegisterView from '../view/users/account/UserAccountRegisterView'
import UserInfoView from '../view/users/account/UserInfoView'
import HomeIndexView from '../view/HomeIndexView'
import UserAccountAcWingLogin from '../view/users/account/UserAccountAcWingLogin'
import store from "../store/index"


const routes = [
  {
    path:"/",
    name:'home',
    redirect:"/home/",
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/home/",
    name:"home_index",
    component:HomeIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/pk/",
    name:"pk_index",
    component:PkIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/record/",
    name:"record_index",
    component:RecordIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/record/:recordId/",
    name:"record_content",
    component:RecordContentView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/ranklist/",
    name:"ranklist_index",
    component:RanklistIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: "/user/account/login/",
    name: "user_account_login",
    component: UserAccountLoginView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path: "/user/account/register/",
    name: "user_account_register",
    component: UserAccountRegisterView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path: "/user/account/acwing/web/receive_code/",
    name: "user_account_acwing_web_receive_code",
    component: UserAccountAcWingLogin,
    meta:{
      requestAuth:false,
    }
  },
  {
    path:"/user/bot/",
    name:"user_bot_index",
    component:UserBotIndexView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/user/info/",
    name:"user_info_index",
    component:UserInfoView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path:"/404/",
    name:"notfound_index",
    component:NotFound,
    meta:{
      requestAuth:false,
    }
  },
  {
    path:"/:catchAll(.*)",
    redirect:"/404/"
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.meta.requestAuth&&!store.state.user.is_login){
    next({name:"user_account_login"})
  }else{
    next();
  }
})
export default router
